#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

int main(void)
{
	key_t key;
	int msqid;
	int proj_id;

	key=IPC_PRIVATE;
	msqid=msgget(key,0777);
	if(msqid==-1)
		perror("cannot create message queue");
	else
		printf("1. key=IPC_PRIVATE, message queue msqid= %d\n",msqid);

	proj_id=1;
	key=ftok("/home/program",proj_id);
	if(key==-1)
		perror("cannot generate messge queue key");

	msqid=msgget(key,IPC_CREAT|0777);
        if(msqid==-1)
                perror("cannot create message queue");
	else
        	printf("2. key=%d generated by ftok, message queue msqid= %d\n",key,msqid);
	
	msqid=msgget(key,IPC_CREAT |IPC_EXCL |0777);
	if(msqid==-1)
		perror("cannot create message queue");

	msqid=msgget(key,IPC_CREAT |0777);
	if(msqid==-1)
		perror("cannot create message queue");	
	else
		printf("Access the existing message queue\n");

	return 0;
}
